<script>
export default {
  name:"Footer",
  data(){
    return {

    }
  },
  props:{
    todos: Array,
    selectedAllTodo: Function,
    delFinishedTodos: Function
  },
  methods:{
    change($event){
      // 通过事件对象获取到是否选中
      const flag = $event.target.checked;
      // this.selectedAllTodo(flag);
    },
    finishedTodo(){
      this.delFinishedTodos();
    }
  },
  computed:{
    finishedNum(){
      return this.todos.reduce( (tatal, todo) => {
        // console.log("tatal", tatal);
        // console.log("todo", todo);
        return tatal + (todo.finished ? 1 :0);
      }, 0);
    },
    isChecked:{
      get(){
        return this.finishedNum === this.todos.length && this.todos.length > 0
      },
      set(value){
        console.log("计算属性获取 checkbox 值：", value);
        this.selectedAllTodo(value);
      }
    }
  }
}
</script>

<template>
  <div class="todo-footer">
    <label>
        <input type="checkbox" @change="change" v-model="isChecked">
    </label>
    <span>
        <span>已完成 {{finishedNum}} 件</span>/ 总计 {{todos.length}} 件
    </span>
    <button class="btn btn-warning" @click="finishedTodo">清楚已完成任务</button>
  </div>
</template>

<style scoped>
/* footer */
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
